home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / digital marsC compier / dm / include / Rtlheap.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-02  |  1.8 KB  |  82 lines

  1. #if __SC__ || __RCC__
  2. #pragma once
  3. #endif
  4.  
  5. #ifndef _H_RTLHEAP
  6. #define _H_RTLHEAP
  7.  
  8. #include <stdlib.h>
  9.  
  10. #ifndef _CRTIMP
  11. #if defined(_WIN32) && defined(_DLL)
  12. #define _CRTIMP  __declspec(dllimport)
  13. #else
  14. #define _CRTIMP
  15. #endif
  16. #endif
  17.  
  18. #if __cplusplus
  19. extern "C"
  20. {
  21. #endif
  22. typedef struct {char unused;} *HRTLPOOL;
  23.  
  24. // Create a pool of fixed sized objects
  25. HRTLPOOL _CRTIMP RTLPoolCreate(size_t);
  26.  
  27. // Allocate an item from the pool
  28. void * _CRTIMP RTLPoolAlloc(HRTLPOOL hPool);
  29.  
  30. // Free an item from the pool
  31. void _CRTIMP RTLPoolFree(HRTLPOOL pPool, void *pData);
  32.  
  33. // Destroy the pool
  34. void _CRTIMP RTLPoolDestroy(HRTLPOOL pPool);
  35.  
  36. #if __cplusplus
  37. };
  38. #endif
  39.  
  40. #if __cplusplus
  41.  
  42. // Macros to implement class-specific new/delete using a fixed size pool.
  43. //
  44. // In the header:
  45. //     class CDataRecord
  46. //         {
  47. //         RTL_DECLARE_POOL()
  48. //     public:
  49. //         };
  50. //
  51. // In the source:
  52. //     RTL_IMPLEMENT_POOL(CDataRecord)
  53. //
  54.  
  55. class RTLPoolWrapper
  56.     {
  57. public:
  58.     RTLPoolWrapper(size_t size) : m_size(size), hPool(RTLPoolCreate(size)) {}
  59.     ~RTLPoolWrapper() {RTLPoolDestroy(hPool);}
  60.     void *Alloc(size_t size) {return size==m_size ? RTLPoolAlloc(hPool) : malloc(size);}
  61.     void Free(void *p, size_t size) {if (size==m_size) RTLPoolFree(hPool, p); else free(p);}
  62. private:
  63.     size_t m_size;
  64.     HRTLPOOL hPool;
  65.     };
  66.  
  67. #define RTL_DECLARE_POOL() \
  68. public:             \
  69.     void *operator new(size_t );    \
  70.     void operator delete(void* , size_t ); \
  71. private:            \
  72.     static RTLPoolWrapper m_RTLPool;
  73.  
  74. #define RTL_IMPLEMENT_POOL(CLASS) \
  75.     RTLPoolWrapper CLASS::m_RTLPool(sizeof(CLASS)); \
  76.     void *CLASS::operator new(size_t n) {return m_RTLPool.Alloc(n);} \
  77.     void CLASS::operator delete(void* p, size_t n) {m_RTLPool.Free(p,n);}
  78.  
  79. #endif
  80.  
  81. #endif
  82.